<script>
import { deleteUserAjax, getUserListAjax } from '@/data/UserinfoData'
import UserinfoEdit from '@/views/userinfo/UserinfoEdit.vue'
import { getRoleListAjax } from '@/data/RoleData'

const searchForm = {}
export default {
  name: 'UserinfoList',
  components: { UserinfoEdit },
  data () {
    return {
      searchForm: { ...searchForm },
      dataList: [],
      roleList: []
    }
  },
  methods: {
    getDataList () {
      const res = getUserListAjax(this.searchForm)
      if (res.code === 200) {
        this.dataList = [...res.data]
      }
    },
    getRoleList () {
      const res = getRoleListAjax()
      if (res.code === 200) {
        this.roleList = [...res.data]
      }
    },
    clear () {
      this.searchForm = { ...searchForm }
      this.getDataList()
    },
    deleteUser (id) {
      this.$confirm(`确定删除编号[${id}]的数据吗?`, { type: 'info' }).then(() => {
        const res = deleteUserAjax(id)
        if (res.code === 200) {
          this.$message.success('删除成功！')
          this.getDataList()
        } else {
          this.$alert(res.msg, { type: 'error' })
        }
      }).catch(() => {})
    },
    showDialog (id) {
      this.$refs.userinfoEdit.getUserInfo(id)
    }
  },
  created () {
    this.getDataList()
    this.getRoleList()
  }
}
</script>

<template>
<div>
  <el-card>
    <el-form inline :model="searchForm" label-width="80px">
      <el-form-item label="用户名">
        <el-input v-model="searchForm.username" placeholder="请输入用户名" clearable/>
      </el-form-item>
      <el-form-item label="昵称">
        <el-input v-model="searchForm.nickname" placeholder="请输入昵称" clearable/>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="searchForm.status" clearable filterable>
          <el-option :value="1" label="可用"/>
          <el-option :value="2" label="禁用"/>
        </el-select>
      </el-form-item>
      <el-form-item label="角色">
        <el-select v-model="searchForm.roleId" clearable filterable>
          <el-option v-for="role in roleList" :key="role.id" :value="role.id" :label="role.name"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList">查询</el-button>
        <el-button @click="clear">清空</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <el-card style="margin-top: 10px">
    <el-button @click="showDialog()" style="margin-bottom: 10px">添加</el-button>
    <el-table border :data="dataList">
      <el-table-column prop="id" label="编号"/>
      <el-table-column prop="username" label="用户名"/>
      <el-table-column prop="nickname" label="昵称"/>
      <el-table-column prop="role.name" label="角色"/>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" type="success">可用</el-tag>
          <el-tag v-else type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="showDialog(scope.row.id)">修改</el-button>
          <el-button type="text" @click="deleteUser(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <userinfo-edit @close="getDataList" ref="userinfoEdit"/>
</div>
</template>

<style scoped>

</style>
